const statusToColor = {
  200: '32',// 绿色
  500: '31',// 红色
  404: '33' // 黄色
}
function blod(str) {
  return '\033[1m' + str + '\033[0m'
}
function handleStatus(status) {
  let key = statusToColor[status] || '0'
  return '\033[' + key + 'm' + status + '\033[0m'
}
function handleTime(ms) {
  let color
  if (ms < 200) {
    color = '\033[32m'
  } else if (ms < 500) {
    color = '\033[33m'
  } else {
    color = '\033[31m'
  }
  return color + ms + 'ms\033[0m'
}
function handleUrl(url) {
  return '\033[4m' + url + '\033[0m'
}
module.exports = function (option) {
  return async (ctx, next) => {
    const start = new Date()
    await next()
    const now = new Date()
    console.log(`${blod(ctx.method)} ${handleUrl(ctx.url)} ${handleStatus(ctx.status)} ${blod(ctx.util.dateFormat(now))} ${handleTime(now - start)}`)
  }
}